package com.xiaobai.netease_news.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.model.ResponseData;
import com.xiaobai.netease_news.utils.ImageUniversalLoaderConfiguration;
import com.xiaobai.netease_news.utils.ViewHolder;

import java.util.List;

/**
 * Created by Android Studio
 * Project: Netease_News
 * Author: Jack
 * Email: jackfangqi1314@gmail.com/jackfangqi@163.com
 * Date: 2015/6/24
 */
public class TopNewsAdapter extends BaseAdapter {

    private Context context;
    private List<ResponseData> responseDatas;
    private LayoutInflater inflater;

    private DisplayImageOptions options = ImageUniversalLoaderConfiguration.initOptions();

    public TopNewsAdapter(Context context, List<ResponseData> responseDatas) {
        this.context = context;
        this.responseDatas = responseDatas;

        if (context != null) {
            inflater = LayoutInflater.from(context);
        }
    }

    @Override
    public int getCount() {
        if (responseDatas != null) {
            return responseDatas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (responseDatas != null) {
            return responseDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = inflater.inflate(R.layout.news_normal_layout, parent, false);

        ImageView newsImg = ViewHolder.getView(convertView, R.id.common_news_img);
        TextView newsTitle = ViewHolder.getView(convertView, R.id.common_news_title);
        TextView newsDigest = ViewHolder.getView(convertView, R.id.common_news_digest);
        TextView replyCount = ViewHolder.getView(convertView, R.id.common_news_reply_count);

        ResponseData responseData = (ResponseData) getItem(position);

        newsTitle.setText(responseData.getTitle());
        newsDigest.setText(responseData.getDigest());
        replyCount.setText(responseData.getReplyCount() + "跟帖");
        ImageLoader.getInstance().displayImage(responseData.getImgsrc(), newsImg, options);

        return convertView;
    }
}
